package com.qjl.shopping.controller;


import com.qjl.shopping.config.EmallConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author liguanghui
 * @Date 2023/4/11
 */
@Slf4j
@RestController
public class fileController {
    @Value("${files.upload.path}")
    private String basePath;
    @Value("${files.upload.act_path}")
    private String actPath;

    @Autowired
    private EmallConfig emallConfig;
    //上传
    @PostMapping("/common/upload")
    public String upload(MultipartFile file){
        String originalFileName = file.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + suffix;
        File dir = new File(basePath);
        if(!dir.exists()){
            dir.mkdir();
        }
        try {
            file.transferTo(new File(basePath+fileName));
        }catch (IOException e){
            e.printStackTrace();
        }
        return fileName;
    }

    //上传活动
    @PostMapping("/common/upload/act")
    public String upload_act(MultipartFile file){
        String originalFileName = file.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + suffix;
        File dir = new File(actPath);
        if(!dir.exists()){
            dir.mkdir();
        }
        try {
            file.transferTo(new File(actPath+fileName));
        }catch (IOException e){
            e.printStackTrace();
        }
        return fileName;
    }

    //    下载
    @GetMapping("/common/download")
    public void download(String name, HttpServletResponse response){
        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/png");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //邮箱
    @GetMapping("emall")
    public void text(){
        emallConfig.sendSimpleMail(
                "1712639109@qq.com");
    }
}


